/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mindwave.theoracle;

import com.csvreader.CsvWriter;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author MarcoJ
 */
public class Writer {

    private CsvWriter csv;
    private String path = "Test\\";
    private String CSV_NAME;
    private double tempo;
    private int imgtmp;
    private boolean p3;
    private double delay = 175;

    public Writer(String name) {

        // INIZIALIZZAZIONE CSVWRITER
        this.CSV_NAME = name;
        File f = new File(path + CSV_NAME + ".csv");
        if (f.exists()) {
            CSV_NAME = CSV_NAME + "_" + Math.round(Math.random() * 1000);
        }
        csv = new CsvWriter(path + CSV_NAME + ".csv");
        csv.setDelimiter(';');
        SetIntestation();
    }

    public void stopWriting() {
        csv.close();
    }

    public void writeArray(experienceBean inp) {
        try {
            if (tempo == 0) {
                tempo = inp.getTime();
                imgtmp = inp.getFigure().getCode();
            }
            if (imgtmp != inp.getFigure().getCode() && p3 == false) {
                tempo = inp.getTime();
                p3 = true;
            }
            if (imgtmp != inp.getFigure().getCode() && p3 == true) {
                inp.setFigure(Figure.getFigure(imgtmp));
                 csv.writeRecord(inp.getStringArray());
                if ((inp.getTime() - tempo) > delay) {
                    p3 = false;
                    imgtmp = inp.getFigure().getCode();
                }

            } else {

                csv.writeRecord(inp.getStringArray());

            }


        } catch (IOException ex) {
            Logger.getLogger(Writer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void SetIntestation() {
        int i = 0;
        String[] temp = new String[16];
        for (Wave wave : Wave.values()) {
            temp[i] = wave + "";
            i++;
        }
        temp[14] = "Figura";
        temp[15] = "Tempo";
        try {
            csv.writeRecord(temp);




        } catch (IOException ex) {
            Logger.getLogger(Writer.class
                    .getName()).log(Level.SEVERE, null, ex);
        }
    }
}
